package cn.wang.web.cargo;


import cn.wang.entity.cargo.Contract;
import cn.wang.service.cargo.ContractService;
import cn.wang.utils.Pagination;
import cn.wang.web.BaseAction;
import com.opensymphony.xwork2.ModelDriven;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * extCproductAction
 * Created by john on 2017/11/1.
 */
@Controller
public class ContractAction extends BaseAction implements ModelDriven<Contract> {
    private Pagination page = new Pagination();
    private List<Contract> ContractList;
    private Contract model = new Contract();
    //获取id
    private String[] id;

    @Autowired
    private ContractService contractService;

    //分页
    public String list() {
        page = contractService.findPage(page.getPageNo(), page.getPageSize(), 0);
        page.setUrl("contractAction_list");
        push(page);
        return "list";
    }

    //查询所有部门
    public String tocreate() {
        ContractList = contractService.findAll();
        return "tocreate";
    }

    //查寻单个部门信息
    public String toview() {
        model = contractService.findById(model.getId());
        push(model);
        return "toview";
    }

    //添加部门
    public String insert() {
        contractService.save(model);
        return "rlist";

    }

    //更新合同 先查询老数据
    public String toupdate() {
        //根据id查询老shuju1
        model = contractService.findById(model.getId());
        push(model);
        return "toupdate";
    }

    public String update() {
        contractService.update(model);
        return "rlist";
    }

    //删除部门 多个部门删除时 id 字符类型id会 拼接在一起(,空格)
    public String delete() {
        if (model.getId() == null) {
            return "rlist";
        }
        String[] ids = model.getId().split(", ");
        contractService.delete(ids);
        return "rlist";
    }

    //提交 修改状态
    public String submit() {
        String[] ids = model.getId().split(", ");
        contractService.updateById(ids);
        return "rlist";
    }

    //打印
    public String print() {

        return null;
    }

    public void setPage(Pagination page) {
        this.page = page;
    }

    public List<Contract> getContractList() {
        return ContractList;
    }

    @Override
    public Contract getModel() {
        //将字段名设为model使一行代码两用 1模型驱动 2 字段set方法
        return model;
    }

    public void setId(String[] id) {
        this.id = id;
    }

}
